<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Incoming;


use App\Http\Controllers\Controller;
use App\Models\Bank;
use App\Models\ProvinceCity;
use App\Models\ProvinceCityArea;
use App\Models\VbillStoreCategory;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class CommonController extends Controller
{
    use ResponseTrait;

    public function city(Request $request): string
    {
        $areaCode = $request->get('area_code', 1); //编号
        $type = $request->get('type', 2); //类型,2-随行付省市区
        if ($type == 2) {
            $city = Cache::get('vbill_city_' . $areaCode);
            if (!$city) {
                $city = ProvinceCityArea::where('area_parent_id', $areaCode)->select('area_code', 'area_name', 'area_parent_id', 'area_type', 'dada_city_code')->get();
                Cache::put('vbill_city_' . $areaCode, $city, 1000000);
            }
        } else {
            $city = Cache::get('city_' . $areaCode);
            if (!$city) {
                $city = ProvinceCity::where('area_parent_id', $areaCode)->select('area_code', 'area_name', 'area_parent_id', 'area_type')->get();
                Cache::put('city_' . $areaCode, $city, 1000000);
            }
        }
        return $this->successResponse("ok", ["data" => $city]);
    }

    public function store_type(): string
    {
        $data = [
            [
                'store_type' => 1,
                'store_type_desc' => '个体工商户',
            ],
            [
                'store_type' => 2,
                'store_type_desc' => '企业',
            ],
            [
                'store_type' => 3,
                'store_type_desc' => '小微商户',
            ],
            [
                'store_type' => 4,
                'store_type_desc' => '学校-教育行业',
            ],
        ];
        return $this->successResponse("ok", ["data" => $data]);
    }

    public function vbill_store_category(Request $request): string
    {
        $level = $request->get('level', '3');
        $category_name = $request->get('categoryName', '');

        $where = [];
        if ($level) {
            $where[] = ['level', '=', $level];
        }
        if ($category_name) {
            $where[] = ['category_name', 'like', '%' . $category_name . '%'];
        }

//        $is_exist = Cache::has('vbill_store_category_'.$level);
//        if ($is_exist) {
//            $data = Cache::get('vbill_store_category_'.$level);
//        } else {
        $data = VbillStoreCategory::query()->where($where)
            ->where('business_type', '线下')
            ->orderby("is_top","desc")
            ->get();
//            Cache::put('vbill_store_category_'.$level, $data, 10000);
//        }
        return $this->successResponse("ok", ["data" => $data]);
    }

    public function bank(Request $request)
    {

        try {
            $keyword = $request->get('bankname');
            $company = $request->get('company', '');

            $where = [];

            if ($company == "dlb") {
                if ($keyword) {
                    $where[] = ['bank_name', 'like', '%' . $keyword . '%'];
                }

                $data = DB::table('dlb_bank')
                    ->select('bank_name as bankname')
                    ->where($where)
                    ->get();
            } else {
                if ($keyword) {
                    $where[] = ['bankname', 'like', '%' . $keyword . '%'];
                }
                $data = Bank::select('bankname')
                    ->where($where)
                    ->get();
            }
            return $this->successResponse("ok", ['data' => $data]);
        } catch (\Exception $exception) {
            return $this->errorResponse($exception->getMessage());
        }
    }


    //查询银行信息
    public function sub_bank(Request $request)
    {
        try {
            $page = $request->get('page', 1);
            $pagesize = $request->get('pagesize', 500);

            $bank_name = $request->get('bank_name', '');
            $sub_bank_keyword = $request->get('sub_bank_keyword', '');
            $bank_area_name = $request->get('bank_area_name', '');
            $bank_city_name = $request->get('bank_city_name', '');
            $bank_province_name = $request->get('bank_province_name', '');


            $bank_province_name = str_replace('省', '', $bank_province_name);
            $bank_city_name = str_replace('市', '', $bank_city_name);
            $bank_area_name = str_replace('区', '', $bank_area_name);
            $bank_area_name = str_replace('县', '', $bank_area_name);


            $company = $request->get('company', '');

            if ($company == "dlb") {
                if (env('DB_D1_HOST')) {
                    $obj = DB::connection("mysql_d1")->table("dlb_sub_bank");
                } else {
                    $obj = DB::table('dlb_sub_bank');
                }

                if ($sub_bank_keyword) {
                    $obj = $obj
                        ->where('b', 'like', '%' . $bank_name . '%' . '%' . $sub_bank_keyword . '%')
                        ->select('a  as bank_no', 'b as sub_bank_name');
                } else {
                    $obj = $obj
                        ->orWhere('b', 'like', $bank_name . $bank_province_name . '%')
                        ->orWhere('b', 'like', $bank_name . $bank_city_name . '%')
                        ->orWhere('b', 'like', $bank_name . $bank_area_name . '%')
                        ->orWhere('b', 'like', $bank_province_name . $bank_name . '%')
                        ->orWhere('b', 'like', $bank_city_name . $bank_name . '%')
                        ->orWhere('b', 'like', $bank_area_name . $bank_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_name . $bank_province_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_name . $bank_city_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_name . $bank_area_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_province_name . $bank_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_city_name . $bank_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_area_name . $bank_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_name . '%' . $bank_province_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_name . '%' . $bank_city_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_name . '%' . $bank_area_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_province_name . '%' . $bank_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_city_name . '%' . $bank_name . '%')
                        ->orWhere('b', 'like', '%' . $bank_area_name . '%' . $bank_name . '%')
                        ->select('a as bank_no', 'b as sub_bank_name');
                }
            } else {
                if (env('DB_D1_HOST')) {
                    $obj = DB::connection("mysql_d1")->table("bank_info");
                } else {
                    $obj = DB::table('bank_info');
                }

                if ($sub_bank_keyword) {
                    $obj = $obj
                        ->where('bankName', 'like', '%' . $bank_name . '%' . '%' . $sub_bank_keyword . '%')
                        ->select('instOutCode  as bank_no', 'bankName as sub_bank_name');
                } else {
                    $obj = $obj
                        ->orWhere('bankName', 'like', $bank_name . $bank_province_name . '%')
                        ->orWhere('bankName', 'like', $bank_name . $bank_city_name . '%')
                        ->orWhere('bankName', 'like', $bank_name . $bank_area_name . '%')
                        ->orWhere('bankName', 'like', $bank_province_name . $bank_name . '%')
                        ->orWhere('bankName', 'like', $bank_city_name . $bank_name . '%')
                        ->orWhere('bankName', 'like', $bank_area_name . $bank_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_name . $bank_province_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_name . $bank_city_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_name . $bank_area_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_province_name . $bank_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_city_name . $bank_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_area_name . $bank_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_name . '%' . $bank_province_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_name . '%' . $bank_city_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_name . '%' . $bank_area_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_province_name . '%' . $bank_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_city_name . '%' . $bank_name . '%')
                        ->orWhere('bankName', 'like', '%' . $bank_area_name . '%' . $bank_name . '%')
                        ->select('instOutCode as bank_no', 'bankName as sub_bank_name');
                }
            }

            $this->t = $obj->count();
            $data = $obj->offset(($page - 1) * $pagesize)->limit($pagesize)->get()->toArray();
            return $this->successResponse("ok", [
                "total" => $obj->count(),
                "list" => $data
            ]);
        } catch (\Exception $exception) {
            return $this->errorResponse($exception->getMessage() . $exception->getLine());
        }
    }


    public function doc()
    {
        $doc = DB::table("extension_worker_doc")->where(["id" => 1])->first();
        return $this->successResponse("ok", ["doc" => empty($doc->context) ? "" : $doc->context]);
    }

}
